home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / SAT 2.3.8 / Libraries & Documentation / Tutorial ƒ / Assignment6.c < prev    next >
C/C++ Source or Header  |  1995-08-10  |  4KB  |  182 lines

  1. // Assignment 6
  2.  
  3. #include "SAT.h"
  4.  
  5. short kSpeed = 5;
  6. SpritePtr ignore;
  7. Handle theSound;
  8. long score;
  9. long startTime;
  10. Boolean doneFlag;
  11. short direction;
  12.  
  13. pascal void SetupTarget (SpritePtr me);
  14.  
  15. pascal void HandleSprite (SpritePtr me)
  16. {
  17.     EventRecord event;
  18.     char temp;
  19.  
  20.     // Now hold on to the hat! I'm using GetOSEvent to get key down events.
  21.     // the keydowns then affect the speed variable in the sprite. That speed
  22.     // is added to the position. Finally, I check the position against the screen
  23.     // borders, and modify the speed in case I reach a border.
  24.  
  25.     if (GetOSEvent(keyDownMask, &event))
  26.     {
  27.         if (event.what = keyDown)
  28.         {
  29.             temp = BitAnd(event.message, charCodeMask);
  30.             
  31.             switch (temp)
  32.             {
  33.                 case 'a': 
  34.                 {
  35.                     me->speed.h = me->speed.h - 1;
  36.                 }
  37.                 break;
  38.                 
  39.                 case 's':
  40.                 {
  41.                     me->speed.h = me->speed.h + 1;
  42.                 }
  43.                 break;
  44.                 
  45.                 case 'w': 
  46.                 {
  47.                     me->speed.v = me->speed.v - 1;
  48.                 }
  49.                 break;
  50.                 
  51.                 case 'z': 
  52.                 {
  53.                     me->speed.v = me->speed.v + 1;
  54.                 }
  55.                 break;
  56.             }
  57.         }
  58.     }
  59.  
  60.     me->position.h = me->position.h + me->speed.h;
  61.     me->position.v = me->position.v + me->speed.v;
  62.     if (me->position.h < 0)
  63.     {
  64.         me->speed.h = !(me->speed.h);
  65.         me->position.h = 0;
  66.     }
  67.     if (me->position.h > gSAT.offSizeH - 32)
  68.     {
  69.         me->speed.h = !(me->speed.h);
  70.         me->position.h = gSAT.offSizeH - 32;
  71.     }
  72.     if (me->position.v < 0)
  73.     {
  74.         me->speed.v = !(me->speed.v);
  75.         me->position.v = 0;
  76.     }
  77.     if (me->position.v > gSAT.offSizeV - 32)
  78.     {
  79.         me->speed.v = !(me->speed.v);
  80.         me->position.v = gSAT.offSizeV - 32;
  81.     }
  82. }
  83.  
  84. pascal void SetupSprite (SpritePtr me)
  85. {
  86.     me->task = &HandleSprite;
  87.     me->face = SATGetFace(128);
  88.     SetRect(&me->hotRect, 0, 0, 32, 32);
  89. }
  90.  
  91. pascal void HandleTarget (SpritePtr me)
  92. {
  93.     // The target sprite isn't modified much. I just limit movement after gSAT.offSizeH instead
  94.     // of a hard-coded constant.
  95.     
  96.     me->position.h = me->position.h + direction;
  97.     if (me->position.h < 0)
  98.         direction = kSpeed;
  99.     if (me->position.h > gSAT.offSizeH - 32)
  100.         direction = -kSpeed;
  101. }
  102.  
  103. pascal void HitTarget (SpritePtr me, SpritePtr him)
  104. {
  105.     SATPort savePort;
  106.     short strLength;
  107.     Str255 str;
  108.     Rect r;
  109.  
  110.     if (him->task = &HandleSprite) // Check what we hit!
  111.     {
  112.         me->task = nil;
  113.         ignore = SATNewSprite(-1, 0, SATRand(gSAT.offSizeV - 32), &SetupTarget);
  114.         
  115.         // We could also re-use the old sprite for a new one, if we like.
  116.         
  117.         SATSoundPlay(theSound, 1, true);
  118.         
  119.         // Add to the score
  120.         
  121.         score = score + 1;
  122.         
  123.         // Draw the score on the screen.
  124.         
  125.         SATGetPort(&savePort); // Save port and device!
  126.         SATSetPortBackScreen();
  127.         SetRect(&r, 100, 0, 100 + 100, 15);
  128.         EraseRect(&r);
  129.         MoveTo(r.left, r.bottom - 3);
  130.         DrawString("\pCaught: ");
  131.         SATDrawLong(score);
  132.         SATBackChanged(&r);
  133.         SATSetPort(&savePort); // Always restore!
  134.  
  135.         if (score == 10)
  136.                 doneFlag = true;
  137.     }
  138. }
  139.  
  140. pascal void SetupTarget (SpritePtr me)
  141. {
  142.     me->task = &HandleTarget;
  143.     me->hitTask = &HitTarget;
  144.     me->face = SATGetFace(129);
  145.     SetRect(&me->hotRect, 0, 0, 32, 32);
  146.     direction = kSpeed;
  147. }
  148.  
  149. main()
  150. {
  151.     short kTicksPerFrame = 2;
  152.     long t;
  153.     Str255 str;
  154.     
  155.     SATInitToolbox();
  156.  
  157.     SATConfigure(false, kVPositionSort, kForwardCollision, 32);
  158.     SATInit(128, 129, 478, 302);
  159.     ignore = SATNewSprite(0, 200, 200, &SetupSprite);
  160.     ignore = SATNewSprite(0, 0, SATRand(gSAT.offSizeV), &SetupTarget);
  161.     theSound = SATGetNamedSound("\pTestSound");
  162.     score = 0;
  163.     HideCursor();
  164.     startTime = TickCount();
  165.     doneFlag = false;
  166.     do
  167.     {
  168.             t = TickCount();
  169.             SATRun(true);
  170.             while ((TickCount () - t) < kTicksPerFrame);
  171.     }while (!Button () && !doneFlag);
  172.     
  173.     ShowCursor();
  174.     
  175.     // Extremely simple result report. A real game should, of course, display this in the game
  176.     // window, in a prettier way, perhaps with a high score list, etc.
  177.  
  178.     sprintf(str, "Time to catch  %ld disks: %ld seconds.", score, (TickCount() - startTime) / 60);
  179.     c2pstr((char *)str);
  180.     SATReportStr(str);
  181.     SATSoundShutup();
  182. }